package lg.minecraft.plugin.worldprotect.gui;

import lg.minecraft.plugin.library.inventory.CancelInventoryContext;
import lg.minecraft.plugin.library.inventory.ClickInventoryContext;
import lg.minecraft.plugin.library.inventory.InventoryContext;
import lg.minecraft.plugin.library.inventory.LGInventory;
import org.bukkit.GameRule;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import java.util.ArrayList;
import java.util.List;

/**
 * @author langle__
 * @version 1.0
 */
public class GameRuleGUI extends LGInventory {

    private static final int[] GLASS_ARRAY = {45, 46, 47, 51, 52, 53};
    private static final int PAGE_SIZE = 45;

    private final World world;
    private final String[] gameRuleArray;
    private final int totalPages;

    private int page = 0; // 当前页码

    public GameRuleGUI(World world) {
        super(6, "§f游戏规则 §7| §f" + world.getName());
        this.world = world;
        this.gameRuleArray = world.getGameRules();
        this.totalPages = (gameRuleArray.length + PAGE_SIZE - 1) / PAGE_SIZE;
    }

    @Override
    public void onInventoryOpen(CancelInventoryContext context) {
        inventory.clear();

        int startIndex = page * PAGE_SIZE;
        int endIndex = Math.min(startIndex + PAGE_SIZE, gameRuleArray.length);

        for (int i = 0; i < endIndex - startIndex; i++) {
            String gameRuleName = gameRuleArray[startIndex + i];
            GameRule<?> gameRule = GameRule.getByName(gameRuleName);
            if (gameRule == null) continue;
            Object value = world.getGameRuleValue(gameRule);
            Material material = null;
            if (value instanceof Integer) {
                material = Material.BLUE_WOOL;
            } else if (value instanceof Boolean) {
                material = (boolean) value ? Material.GREEN_WOOL : Material.RED_WOOL;
            }
            if (material == null) continue;
            ItemStack item = new ItemStack(material);
            ItemMeta meta = item.getItemMeta();
            if (meta != null) {
                meta.setDisplayName("§f" + gameRuleName);
                List<String> lore = new ArrayList<>();
                lore.add("§f" + value);
                meta.setLore(lore);
                item.setItemMeta(meta);
            }
            inventory.setItem(i, item);
        }

        ItemStack borderItem = new ItemStack(Material.GRAY_STAINED_GLASS_PANE);
        ItemMeta borderMeta = borderItem.getItemMeta();
        if (borderMeta != null) {
            borderMeta.setDisplayName("§r");
            borderItem.setItemMeta(borderMeta);
        }
        for (int slot : GLASS_ARRAY) inventory.setItem(slot, borderItem);

        ItemMeta meta;

        ItemStack backItem = new ItemStack(Material.RED_STAINED_GLASS_PANE);
        if ((meta = backItem.getItemMeta()) != null) {
            meta.setDisplayName("§c返回");
            backItem.setItemMeta(meta);
        }

        ItemStack prevItem = new ItemStack(Material.LIME_STAINED_GLASS_PANE);
        if ((meta = prevItem.getItemMeta()) != null) {
            meta.setDisplayName("§a上一页");
            prevItem.setItemMeta(meta);
        }

        ItemStack nextItem = new ItemStack(Material.LIME_STAINED_GLASS_PANE);
        if ((meta = nextItem.getItemMeta()) != null) {
            meta.setDisplayName("§a下一页");
            nextItem.setItemMeta(meta);
        }

        inventory.setItem(49, backItem);
        inventory.setItem(48, prevItem);
        inventory.setItem(50, nextItem);
    }

    @Override
    public void onInventoryClick(ClickInventoryContext context) {
        context.setCancelled(true);
        Player player = context.getPlayer();
        int slot = context.getSlot();

        if (slot == 49) {
            new MainGUI().openInventory(player);
            player.playSound(player.getLocation(), Sound.UI_BUTTON_CLICK, 0.5F, 1F);
        } else if (slot == 48 && page > 0) {
            page--;
            onInventoryOpen(context);
            player.playSound(player.getLocation(), Sound.UI_BUTTON_CLICK, 0.5F, 1F);
        } else if (slot == 50 && page < totalPages - 1) {
            page++;
            onInventoryOpen(context);
            player.playSound(player.getLocation(), Sound.UI_BUTTON_CLICK, 0.5F, 1F);
        } else if (slot >= 0 && slot < PAGE_SIZE) {
            int index = slot + page * PAGE_SIZE;
            if (index >= gameRuleArray.length) return;
            String gameRuleName = gameRuleArray[index];
            final GameRule<?> gameRule = GameRule.getByName(gameRuleName);

            if (gameRule == null) return;
            Object value = world.getGameRuleValue(gameRule);

            if (value instanceof Integer) {
                GameRule<Integer> rule = (GameRule<Integer>) gameRule;
                int intValue = (int) value;
                ClickType click = context.getClick();
                if (click.isLeftClick() && !click.isShiftClick()) {
                    intValue++;
                } else if (click.isRightClick() && !click.isShiftClick()) {
                    intValue--;
                } else if (click.isLeftClick()) {
                    intValue += 10;
                } else if (click.isRightClick()) {
                    intValue -= 10;
                }
                world.setGameRule(rule, intValue);
                player.playSound(player.getLocation(), Sound.ENTITY_EXPERIENCE_ORB_PICKUP, 1.0f, 1.0f);
                onInventoryOpen(context);
            } else if (value instanceof Boolean) {
                GameRule<Boolean> rule = (GameRule<Boolean>) gameRule;
                world.setGameRule(rule, !((boolean) value));
                player.playSound(player.getLocation(), Sound.ENTITY_VILLAGER_YES, 1, 1);
                onInventoryOpen(context);
            }
        }
    }

    @Override
    public void onInventoryClose(InventoryContext context) {
    }

}
